using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

namespace Janus.NorthwindDemo2007
{
	public partial class NorthwindApp
	{
		private ConfigDataSet mConfigDS;
		private bool mReadSettingFromDS;
		private const string ConfigFileName = "NWindApp.nws";
		public static ConfigDataSet ConfigDataSet
		{
			get
			{
				return (ConfigDataSet)NorthwindApplication.mConfigDS;
			}
		}
		public static ConfigDataSet.ApplicationSettingsRow AppSettings
		{
			get
			{
				return (ConfigDataSet.ApplicationSettingsRow)NorthwindApplication.mConfigDS.ApplicationSettings.Rows[0];
			}
		}
		public static bool ReadSettingsFromDS
		{
			get
			{
				return NorthwindApplication.mReadSettingFromDS;
			}
		}
		public static void SaveAppSettings()
		{
			string strDataFile = GetLayoutPath(ConfigFileName);
			RootFolder.SaveViewSettings(ConfigDataSet.FolderViews);
			NorthwindApplication.mConfigDS.WriteXml(strDataFile);
		}
		public static DataProvider ActiveViewDataProvider
		{
			get
			{
				if (MainForm.CurrentFolder != null)
				{
					return MainForm.CurrentFolder.DataProvider;                    
				}
				else
				{
					return null;
				}
			}
		}
		public static DataProvider GetDataProvider(Type providerType)
		{
			if (ActiveViewDataProvider != null)
			{
				if (ActiveViewDataProvider.GetType() == providerType)
				{
					return ActiveViewDataProvider;
				}
			}
			return (DataProvider)(Activator.CreateInstance(providerType));
		}
  
		
		public static string LayoutPath(bool create)
		{
			System.IO.FileInfo fInfo = new System.IO.FileInfo(Application.ExecutablePath);
			string strLayoutDirName = System.IO.Path.Combine(fInfo.DirectoryName, "AppSettings");
			if (create)
			{
				System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(strLayoutDirName);
				if (! dirInfo.Exists)
				{
					dirInfo.Create();
				}
			}
			return strLayoutDirName;
		}
		public static string GetLayoutPath(string layoutName)
		{
			return System.IO.Path.Combine(Janus.NorthwindDemo2007.NorthwindApp.LayoutPath(true), layoutName);
		}

		public static string MessageCaption = "Janus Northwind Demo";
		private MainForm mMainForm;
		private RootFolder mRootFolder;
		public static MainForm MainForm
		{
			get
			{
				if (mApplication == null)
				{
					return null;
				}
				return mApplication.mMainForm;
			}
		}
		public static RootFolder RootFolder
		{
			get
			{
				return mApplication.mRootFolder;
			}
		}
		private static NorthwindApp mApplication;
		public static void Initialize()
		{
			frmSplashScreen splash = new frmSplashScreen();
			splash.Show();
			splash.Update();
			mApplication = new NorthwindApp();
			//Initialize root folder

			mApplication.mRootFolder = new RootFolder();
			//create and initialize main form
			mApplication.mMainForm = new MainForm();
			mApplication.mMainForm.InitializeMainForm();

			try
			{
				splash.Close();
				splash.Dispose();
                Application.Run(mApplication.mMainForm);
			}
			catch (Exception exc)
			{
				MessageBox.Show(exc.ToString());
			}
            //Janus.NorthwindDemo2007.NorthwindApp.RootFolder.CalendarFolder.UpdateData();
		}

		public void LoadCalendar()
		{
			this.mRootFolder.CalendarFolder.Load();
		}

		public static string GetRecurrenceInfoText(Janus.Windows.Schedule.RecurrencePattern pattern)
		{
			string recurrenceText = "Occurs every ";
			switch (pattern.RecurrenceType)
			{
				case Janus.Windows.Schedule.RecurrenceType.Daily:
					switch (pattern.DailyRecurrenceMode)
					{
						case Janus.Windows.Schedule.DailyRecurrenceMode.UseInterval:
							if (pattern.Interval == 1)
							{
								recurrenceText += "day";
							}
							else
							{
								recurrenceText += pattern.Interval + " days";
							}
							break;
						case Janus.Windows.Schedule.DailyRecurrenceMode.WeekDays:
							recurrenceText += "weekday";
							break;
						case Janus.Windows.Schedule.DailyRecurrenceMode.WeekendDays:
							recurrenceText += "weekend day";
							break;
					}
					break;
				case Janus.Windows.Schedule.RecurrenceType.Weekly:
					if (pattern.Interval > 1)
					{
						recurrenceText += pattern.Interval + " weeks on ";
					}
					recurrenceText += GetDayOfWeekText(pattern.DayOfWeek);
					break;
				case Janus.Windows.Schedule.RecurrenceType.Monthly:
					if (pattern.Interval > 1)
					{
						recurrenceText += "day " + pattern.DayOfMonth + " of every month";
					}
					else
					{
						recurrenceText += "day " + pattern.DayOfMonth + " of every " + pattern.Interval + " months";
					}
					break;
				case Janus.Windows.Schedule.RecurrenceType.MontNth:
					recurrenceText += pattern.OccurrenceInMonth + " " + GetDayOfWeekText(pattern.DayOfWeek);
					if (pattern.Interval > 1)
					{
						recurrenceText += " of every month";
					}
					else
					{
						recurrenceText += " of every " + pattern.Interval + " months";
					}
					break;
				case Janus.Windows.Schedule.RecurrenceType.Yearly:
					recurrenceText += System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(pattern.MonthOfYear) + " " + pattern.DayOfMonth;
					break;
				case Janus.Windows.Schedule.RecurrenceType.YearNth:
					recurrenceText += pattern.OccurrenceInMonth + " " + GetDayOfWeekText(pattern.DayOfWeek);
					recurrenceText += " of " + System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(pattern.MonthOfYear);

					break;
			}
			recurrenceText += " effective " + pattern.PatternStartDate.ToShortDateString();
			System.DateTime startTime = DateTime.Today.Add(pattern.StartTime);
			System.DateTime endTime = DateTime.Today.Add(pattern.EndTime);
			recurrenceText += " from " + startTime.ToShortTimeString() + " to " + endTime.ToShortTimeString();
			return recurrenceText;
		}

		private static string GetDayOfWeekText(Janus.Windows.Schedule.ScheduleDayOfWeek daysOfWeek)
		{
			string daysOfWeekText = "";
			if ((daysOfWeek & Janus.Windows.Schedule.ScheduleDayOfWeek.Monday) != 0)
			{
				daysOfWeekText += "Monday, ";
			}
			if ((daysOfWeek & Janus.Windows.Schedule.ScheduleDayOfWeek.Tuesday) != 0)
			{
				daysOfWeekText += "Tuesday, ";
			}
			if ((daysOfWeek & Janus.Windows.Schedule.ScheduleDayOfWeek.Wednesday) != 0)
			{
				daysOfWeekText += "Wednesday, ";
			}
			if ((daysOfWeek & Janus.Windows.Schedule.ScheduleDayOfWeek.Thursday) != 0)
			{
				daysOfWeekText += "Thursday, ";
			}
			if ((daysOfWeek & Janus.Windows.Schedule.ScheduleDayOfWeek.Friday) != 0)
			{
				daysOfWeekText += "Friday, ";
			}
			if ((daysOfWeek & Janus.Windows.Schedule.ScheduleDayOfWeek.Saturday) != 0)
			{
				daysOfWeekText += "Saturday, ";
			}
			if ((daysOfWeek & Janus.Windows.Schedule.ScheduleDayOfWeek.Sunday) != 0)
			{
				daysOfWeekText += "Sunday, ";
			}
			daysOfWeekText = daysOfWeekText.Remove(daysOfWeekText.Length - 2, 2);
			return daysOfWeekText;
		}

		public static string GetColumnFriendlyName(Janus.Windows.GridEX.GridEXColumn column)
		{
			if (column.Caption.Length == 0)
			{
				if (column.Tag != null)
				{
					return System.Convert.ToString(column.Tag);
				}
				else
				{
					return column.Key;
				}
			}
			else
			{
				return column.Caption;
			}
		}

		public static string GetCustomGroupName(ICollection groupRows, string objName, int index)
		{

			string propName = objName + index;
			foreach (Janus.Windows.GridEX.GridEXCustomGroup customGroup in groupRows)
			{
				if (string.Compare(customGroup.Key, propName, true) == 0)
				{
					return GetCustomGroupName(groupRows, objName, index + 1);
				}
			}

			return propName;

		}

		public static string GetCustomGroupRowName(ICollection groupRows, string objName, int index)
		{

			string propName = objName + index;
			foreach (Janus.Windows.GridEX.GridEXCustomGroupRow groupRow in groupRows)
			{
				if (string.Compare(groupRow.GroupCaption, propName, true) == 0)
				{
					return GetCustomGroupRowName(groupRows, objName, index + 1);
				}
			}

			return propName;

		}
		public static NorthwindApp NorthwindApplication
		{
			get
			{
				return mApplication;
			}
		}
		private bool mCountriesLoaded;
		public DataTable GetCountriesTable()
		{
			if (! mCountriesLoaded)
			{
				this.contriesAdapter.Fill(this.northwindLists.CountriesFlag);
				mCountriesLoaded = true;
			}
			return this.northwindLists.CountriesFlag;
		}
		private bool mEmployeesLoaded;
		public DataTable GetEmployeeList()
		{
			if (! mEmployeesLoaded)
			{
				this.employeeListAdapter.Fill(this.northwindLists.EmployeeList);
				mEmployeesLoaded = true;
			}
			return this.northwindLists.EmployeeList;
		}
		private bool mSuppliersLoaded;
		public DataTable GetSupplierList()
		{
			if (! mSuppliersLoaded)
			{
				this.suppliersListAdapter.Fill(this.northwindLists.SuppliersList);
				mSuppliersLoaded = true;
			}
			return this.northwindLists.SuppliersList;
		}
		private bool mProductCategoriesLoaded;
		public DataTable GetProductCategoryList()
		{
			if (! mProductCategoriesLoaded)
			{
				this.productCategoriesAdapter.Fill(this.northwindLists.Categories);
				mProductCategoriesLoaded = true;
			}
			return this.northwindLists.Categories;
		}
        private bool mCategoriesLoaded;
        public DataTable GetCategoryList()
        {
            if (!mCategoriesLoaded)
            {
                this.categoriesAdapter1.Fill(this.northwindLists.Categories);
                mCategoriesLoaded = true;
            }
            return this.northwindLists.Categories;
        }
		private bool mCustomersLoaded;
		public DataTable GetCustomersList()
		{
			if (! mCustomersLoaded)
			{
				this.customerListAdapter.Fill(this.northwindLists.CustomerList);
				mCustomersLoaded = true;
			}
			return this.northwindLists.CustomerList;
		}
		private bool mShippersLoaded;
		public DataTable GetShippersList()
		{
			if (! mShippersLoaded)
			{
				this.shippersAdapter.Fill(this.northwindLists.Shippers);
				mShippersLoaded = true;
			}
			return this.northwindLists.Shippers;

		}
		private bool mProductsLoaded;
		public DataTable GetProductList()
		{
			if (! mProductsLoaded)
			{
				this.productListAdapter.Fill(this.northwindLists.ProductList);
				mProductsLoaded = true;
			}
			return this.northwindLists.ProductList;

		}
	}

} //end of root namespace